mini shell code
#include
#include
#include // For system()
using namespace std;
int main() {
string command;
cout << "Mini Shell (type 'exit' to quit)" << endl;
while (true) {
cout << "> "; // Shell prompt
getline(cin, command);
if (command == "exit") {
break;
}
// Execute the command
int ret = system(command.c_str());
if (ret == -1) {
cout << "Failed to execute command." << endl;
}
}
cout << "Exiting mini shell. Bye!" << endl;
return 0;
}
Code output
Mini Shell (type 'exit' to quit)
> echo Hello, world!
Hello, world!
> dir
Volume in drive C has no label.
Volume Serial Number is XXXX-XXXX
Directory of C:\Users\YourUser
07/07/2025 03:45 PM .
07/07/2025 03:45 PM ..
07/06/2025 10:15 AM 1,234 example.txt
1 File(s) 1,234 bytes
2 Dir(s) 100,000,000,000 bytes free
> invalidcommand
'/bin/sh: invalidcommand: command not found' (on Unix)
'or message like "invalidcommand" is not recognized...' (on Windows)
> exit
Exiting mini shell. Bye!